//
//  CourseWorkCreationViewController.m
//  Gradekeeper0.2
//
//  Created by Tim Qian on 09-12-3.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CourseWorkCreationViewController.h"


@implementation CourseWorkCreationViewController

@synthesize nameTextField, typeTextField, markTextField, maxMarkTextField, startDateTextField, dueDateTextField, course, updateButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil course:(Courses*)newCourse {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.course = [[Courses alloc] init];
		NSLog(@"newCourse = %@ %@", [newCourse getName], [self.course getName]);
    }
    return self;
}

- (IBAction) update:(id)sender {
	
	if ([[nameTextField text] length] < 1 ||
		[[markTextField text] length] < 1 ||
		[[maxMarkTextField text] length] < 1 ||
		[[typeTextField text] length] < 1 ||
		[[startDateTextField text] length] < 1 ||
		[[dueDateTextField text] length] < 1){
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Invalid input" 
							  message:@"One or more field is empty, please input." 
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else if ([[nameTextField text] length] > 15 ||
			   [[markTextField text] length] > 3 ||
			   [[maxMarkTextField text] length] > 3) {
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Invalid input" 
							  message:@"One or more exceeds capacity." 
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else {
		if (course != nil) {
			if (TRUE) {
				
				[[course CourseWorks] addObject:[[CourseWork alloc] initWithName:[nameTextField text]
															 Type:[typeTextField text]
															 Mark:[[markTextField text] floatValue]
														  MaxMark:[[maxMarkTextField text] floatValue]
														StartDate:[startDateTextField text]
																		 DueDate:[dueDateTextField text]]];
				UIAlertView *alert = [[UIAlertView alloc] 
									  initWithTitle:@"Invalid input" 
									  message:@"The coursework you entered already exists." 
									  delegate:self
									  cancelButtonTitle:nil
									  otherButtonTitles:nil]; 
				[alert addButtonWithTitle:@"Ok"];
				[alert show];
				[alert release];
			} else {
				NSLog(@"does this run");
				UIAlertView *alert = [[UIAlertView alloc] 
									  initWithTitle:@"Success" 
									  message:@"New coursework has been created" 
									  delegate:self
									  cancelButtonTitle:@"Ok" 
									  otherButtonTitles:nil]; 
				[alert show];
				[alert release];
			}
		}
		
	}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//Create a cancel button that lets the user cancel course creation/edit
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
																			 style:UIBarButtonItemStyleBordered 
																			target:self 
																			action:@selector(cancelCreate:)];
	self.title = [NSString stringWithFormat:@"CourseWork Creation"];
	
	nameTextField.returnKeyType = markTextField.returnKeyType = maxMarkTextField.returnKeyType = typeTextField.returnKeyType = startDateTextField.returnKeyType = dueDateTextField.returnKeyType = UIReturnKeyDone;
}

- (void)cancelCreate:(id)sender {
	//If user confirms cancellation, current view will be popped and return to SemTableView
	UIAlertView *alert = [[UIAlertView alloc] 
						  initWithTitle:@"Cancel Confirmation" 
						  message:@"Are you sure you would like to cancel the action?" 
						  delegate:self
						  cancelButtonTitle:@"Yes" 
						  otherButtonTitles:nil]; 
	[alert addButtonWithTitle:@"No"];
    [alert show];
	
	[alert release];
} //cancelCreate

/*// allow only legal input ( 3 numbers)
 - (BOOL)textField:(UITextField *)textField charSet:(NSString *) set {
 
 NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:set] invertedSet];
 NSString *filtered = [[[textField text] componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
 
 return [string isEqualToString:filtered];
 } //textField*/

//If cancellation is confirmed, pop to root view controller
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView.title isEqualToString:@"Invalid input"]){
	} else { 
		if (buttonIndex == 0) {
			[[self navigationController] popToRootViewControllerAnimated:YES];
		}
    }
} //alertView

//Hides the keyboard after finishing text input (clicking done)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
} //textFieldShouldReturn

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
